package lb.io.utils;

import java.util.Random;

public class Progress {

    private double value;

    public Progress() {
        this.value = 0.0;
    }

    private synchronized void setValue(double value) {
        this.value = value;
    }

    private synchronized double getValue() {
        return value;
    }

    public void onNext() {
        if (getValue() >= 100) {
            setValue(100);
            return;
        }
        double seed;
        if (getValue() < 30) {
            seed = 5.0;
        } else if (getValue() < 60) {
            seed = 4.0;
        } else if (getValue() < 90) {
            seed = 2.0;
        } else if (getValue() < 95) {
            seed = 1.0;
        } else {
            seed = 0;
        }
        setValue(seed * new Random().nextDouble() + getValue());
    }

    private void onCompleted() {
        setValue(100);
    }

    public double progress() {
        return this.getValue();
    }

    public static void main(String[] args) {
        Progress p = new Progress();
        p.onNext();
        System.out.println(p.progress());
        p.onNext();
        System.out.println(p.progress());
        p.onCompleted();
        System.out.println(p.progress());
    }
}
